//
//  ESVideoTableViewCell.m
//  Eyeshot
//
//  Created by lanou3g on 15/7/2.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESVideoTableViewCell.h"
#import "ESVideoModel.h"
#import "NSString+Self_daptionHeight.h"
#import "UIButton+WebCache.h"
#import "AFNetworking.h"

#define URL @"http://api.budejie.com/api/api_open.php?a=love&appname=baisishequ&asid=1880212C-B451-4FA7-8DFA-E07254EFCEE9&c=post&client=iphone&device=iPhone%204S&id=14730244&jbk=0&mac=&market=&openudid=5a299ed728ffa000f0c4db0eb49e93d0e3968b2a&sex=m&tj_from=video&udid=&uid=15094656&ver=3.6"

@interface ESVideoTableViewCell ()<ESToolbarDelegate>
@property (nonatomic,strong) ESCellTopView *topView;
/** 标题 */
@property (nonatomic,strong) UILabel *texLabel;
/** 播放占位图 */
@property (nonatomic,strong) UIButton *bimageuri;
/** 顶 */
@property (nonatomic,strong) UIButton *dingBtn;
/** 踩 */
@property (nonatomic,strong) UIButton *caiBtn;
/** 分享 */
@property (nonatomic,strong) UIButton *repostBtn;
/** 评论 */
@property (nonatomic,strong) UIButton *commentBtn;
/** 播放次数 */
@property (nonatomic,strong) UIButton *playCount;
/** 视频总时间 */
@property (nonatomic,strong) UIButton *playTime;

@end
@implementation ESVideoTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{

    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor colorWithRed: 234/255.0 green:234/255.0 blue: 234/255.0 alpha:1.0];
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, SCREEN_HEIGHT)];
        _bgView.backgroundColor = [UIColor whiteColor];
        
        _topView = [ESCellTopView cellTopView];
        _topView.backgroundColor = [UIColor whiteColor];
        [_bgView addSubview:_topView];
        
        self.texLabel = [[UILabel alloc] init];
        self.texLabel.font = [UIFont systemFontOfSize:15];
        self.texLabel.numberOfLines = 0;
        [_bgView addSubview:_texLabel];
        
        self.bimageuri = [[UIButton alloc] init];
        [self.bimageuri addTarget:self action:@selector(play:) forControlEvents:(UIControlEventTouchUpInside)];
        [_bgView addSubview:_bimageuri];
        
        self.playview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"video-play"]];
        [self.bimageuri addSubview:_playview];
        
        self.playCount = [[UIButton alloc] init];
        [_bgView addSubview:_playCount];
        
        self.playTime = [[UIButton alloc] init];
        [_bgView addSubview:_playTime];
        
        
        //创建指示器
        self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleWhite)];
//        [self.bimageuri addSubview:_indicatorView];
        
        //创建 顶、踩、分享、评论按钮的工具栏
        [self setupToolbar];
        
        [self.contentView addSubview:_bgView];
      
    }
    
    self.selectionStyle =  UITableViewCellSelectionStyleNone;
    return self;
}




//点击头像按钮事件
- (void)handleProfileBtnClick:(UIButton *)btn
{
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(videoCell:didSelectButton:)]) {
        [self.delegate videoCell:self didSelectButton:btn];
    }
}


//图片点击播放
- (void)play:(UIButton *)btn
{
    
        self.playview.hidden = YES;
        [_indicatorView startAnimating];
    
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(myCell:didSelectButton:model:)]) {
        [self.delegate myCell:self didSelectButton:btn model:self.model];
    }
}

- (void)setFrameAndContent
{
    //1.设置贴主信息
  
    _topView.frame = CGRectMake(ZERO, ZERO, _bgView.width, 12 *kVertical);
    _topView.backgroundColor = [UIColor whiteColor];
    
    [_topView.userProfile sd_setImageWithURL:[NSURL URLWithString:_model.profile_image] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"default64"]];
    [_topView.userProfile setTitle:_model.name forState:UIControlStateNormal];
    _topView.userProfile.subLabel.text = _model.passtime;
    
    [_topView.userProfile setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _topView.userProfile.titleLabel.font = [UIFont systemFontOfSize:12];
    _topView.userProfile.titleLabel.textAlignment = NSTextAlignmentLeft;
    _topView.userProfile.subLabel.textColor = [UIColor grayColor];
    _topView.userProfile.subLabel.font = [UIFont systemFontOfSize:10];
    
    //添加事件，跳转到一个用户的详情页面
    [_topView.userProfile addTarget:self action:@selector(intoUser:) forControlEvents:(UIControlEventTouchUpInside)];
    _topView.userProfile.userid = _model.user_id;
    
    //3.设置标题
    
    CGFloat textH = [_model.text sizeWithFont:[UIFont systemFontOfSize:15] maxSize:CGSizeMake(SCREEN_WIDTH - 2*kPading, 0)].height;
    CGFloat textW = SCREEN_WIDTH - 2*kPading;
    self.texLabel.x = 0.5*kPading;
    self.texLabel.y = CGRectGetMaxY(_topView.frame) + kPading;
    self.texLabel.width = textW;
    self.texLabel.height = textH;
    //4.设置播放背景
    
    CGFloat imageW = textW;
    CGFloat originalW = [_model.width floatValue];
    CGFloat originalH = [_model.height floatValue];
     CGFloat imageH = imageW /originalW *originalH;
    //避免视频窗口超出这个手机屏幕
    if (imageH > SCREEN_HEIGHT - BAR_HEIGHT - CATEGORY_BAR_HEIGHT - kPading) {
        imageW = textW - 2*kPading;
        imageH = imageW /originalW *originalH;
        self.bimageuri.x = 1.5*kPading;
    }else{
        self.bimageuri.x = 0.5*kPading;
    }
    self.bimageuri.y = CGRectGetMaxY(self.texLabel.frame) + 0.5*kPading;
    self.bimageuri.width = imageW;
    self.bimageuri.height = imageH;
    [self.bimageuri sd_setBackgroundImageWithURL:[NSURL URLWithString:_model.bimageuri] forState:(UIControlStateNormal) placeholderImage:[UIImage imageNamed:@"zhanwei"]];
    
    //设置播放次数标签
    if ([[NSString stringWithFormat:@"%@",_model.playcount] isEqualToString:@""]) {
   
    if ([_model.playcount isEqualToString:@""]) {
        _model.playcount = @"0";
    }
    CGSize size = [[NSString stringWithFormat:@"%@",_model.playcount] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
    self.playCount.width = size.width + 1.5*kPading;
    self.playCount.adjustsImageWhenHighlighted = NO;
    self.playCount.height = 0.8*kPading;
    self.playCount.y = self.bimageuri.y;
    self.playCount.x = _bgView.width - self.playCount.width;
    NSString *content = [NSString stringWithFormat:@"%@播放",_model.playcount];
    [self.playCount setTitle:content forState:(UIControlStateNormal)];
    self.playCount.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.playCount setBackgroundImage:[UIImage imageNamed:@"mainCellPopupButtonMiddleN"] forState:(UIControlStateNormal)];
    //设置播放时间标签
    int allTime = [_model.videotime intValue];
    int minute = allTime / 60;
    int second = allTime % 60;
    NSString *timeStr = [NSString stringWithFormat:@"%.2d:%.2d",minute,second];
     CGSize sizeTime = [timeStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
    self.playTime.width = sizeTime.width;
    self.playTime.height = 0.8*kPading;
     self.playTime.adjustsImageWhenHighlighted = NO;
    self.playTime.y = CGRectGetMaxY(_bimageuri.frame) - 0.8*kPading;
    self.playTime.x = _bgView.width - sizeTime.width;
    [self.playTime setTitle:timeStr forState:UIControlStateNormal];
    self.playTime.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.playTime setBackgroundImage:[UIImage imageNamed:@"mainCellPopupButtonMiddleN"] forState:(UIControlStateNormal)];
    
    //设置播放按钮frame
    self.playview.center = CGPointMake(imageW/2, imageH/2);
    //设置指示器frame
    self.indicatorView.center = CGPointMake(imageW/2, imageH/2);
//     [_indicatorView startAnimating];
    //4.设置工具栏
    self.toolbar.width = _bgView.width;
    self.toolbar.height = 44;
    self.toolbar.x = 0;
    self.toolbar.y = CGRectGetMaxY(self.bimageuri.frame) + 0.5 *kPading;
    [self modifyBtnTitle:_model.love type:ESToolbarBtnTypeDing];
    [self modifyBtnTitle:_model.cai type:ESToolbarBtnTypeCai];
    [self modifyBtnTitle:_model.repost type:ESToolbarBtnTypeShare];
    [self modifyBtnTitle:_model.comment type:ESToolbarBtnTypeComment];
    
    //5.设置cell背景图的frame
#warning self.bgView.width 要用self.height  不能用self.contentView.height
    CGFloat bgHeight = CGRectGetMaxY(self.toolbar.frame) + 5;
//    self.bgView.frame = CGRectMake(0.5*kPading, 0.5*kPading, SCREEN_WIDTH - kPading, self.height - 0.5*kPading);
      self.bgView.frame = CGRectMake(0.5*kPading, 0.5*kPading, SCREEN_WIDTH - kPading, bgHeight);
}
}

//进入用户详情控制器
- (void)intoUser:(ESCellUserProfile *)btn
{
    if ([_delegate respondsToSelector:@selector(videoTableViewCell:didSeletedUserBtn:)])
    {
        [_delegate videoTableViewCell:self didSeletedUserBtn:btn];
    }
    
    
}


//如果工具栏对应按钮有数据，显示对应数据
- (void)modifyBtnTitle:(NSString *)title type:(ESToolbarBtnType) type
{
    if (![title isEqualToString:@"0"]) {
        [((UIButton *)[self.contentView viewWithTag:type]) setTitle:title forState:(UIControlStateNormal)];
        [((UIButton *)[self.contentView viewWithTag:type]) setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    }
    
}

- (void)setupToolbar
{
    self.toolbar = [ESToolbar toolbar];
    [_bgView addSubview:_toolbar];
}


- (void)setModel:(ESVideoModel *)model
{
    _model = model;
     NSString *count = [NSString stringWithFormat:@"%@",model.playcount];
    _model.playcount = count;
    self.texLabel.text = model.text;
    [self setFrameAndContent];
}

+ (CGFloat)getHeightForCellByModel:(ESVideoModel *)model
{
    CGFloat padding = 3.5 * kPading;
    CGFloat iconH = 1.5 * kPading;
    CGFloat textH = [model.text sizeWithFont:[UIFont systemFontOfSize:15] maxSize:CGSizeMake(SCREEN_WIDTH - 2*kPading, 0)].height;
    CGFloat originalW = [model.width floatValue];
    CGFloat originalH = [model.height floatValue];
    CGFloat imageH = (SCREEN_WIDTH - 2*kPading)/ originalW * originalH;
    CGFloat toolbarH = 44;
    if (imageH > SCREEN_HEIGHT - BAR_HEIGHT - CATEGORY_BAR_HEIGHT - kPading) {
        CGFloat imageH = (SCREEN_WIDTH - 4*kPading)/ originalW * originalH;
        return padding + iconH + textH + imageH +toolbarH;
    }else
    {
        return padding + iconH + textH + imageH +toolbarH;
    }
    
}



@end
